﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using NiuX.Dapper.Dapper;

namespace NiuX.Dapper.Repositories;

/// <summary>
/// 非泛型 Dapper 仓储
/// </summary>
public partial class DapperRepository : IDapperRepository, IDisposable
{
    /// <summary>
    /// 数据库连接对象
    /// </summary>
    private readonly IDbConnection _db;

    /// <summary>
    /// 服务提供器
    /// </summary>
    private readonly IServiceProvider _serviceProvider;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="serviceProvider"></param>
    /// <param name="db"></param>
    public DapperRepository(IServiceProvider serviceProvider, IDbConnection db)
    {
        _serviceProvider = serviceProvider;
        _db = db;
    }

    /// <summary>
    /// 连接上下文
    /// </summary>
    public virtual IDbConnection DbConnection
    {
        get
        {
            if (_db.State != ConnectionState.Open) _db.Open();
            return _db;
        }
    }


    /// <summary>
    /// 查询返回动态类型
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual IEnumerable<dynamic> Query(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.Query(sql, param, transaction, buffered, commandTimeout, commandType);
    }

    public List<dynamic> QueryAsList(string sql, object param = null, IDbTransaction transaction = null,
        bool buffered = true,
        int? commandTimeout = null, CommandType? commandType = null) =>
        Query(sql, param, transaction, buffered, commandTimeout, commandType).AsList();

    /// <summary>
    /// 查询返回特定类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="buffered"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual IEnumerable<T> Query<T>(string sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.Query<T>(sql, param, transaction, buffered, commandTimeout, commandType);
    }

    /// <summary>
    /// 查询返回动态类型
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual Task<IEnumerable<dynamic>> QueryAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.QueryAsync(sql, param, transaction, commandTimeout, commandType);
    }

    /// <summary>
    /// 查询返回特定类型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.QueryAsync<T>(sql: sql, param, transaction, commandTimeout, commandType);
    }

    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual int Execute(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.Execute(sql, param, transaction, commandTimeout, commandType);
    }

    /// <summary>
    /// 执行命令
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <param name="transaction"></param>
    /// <param name="commandTimeout"></param>
    /// <param name="commandType"></param>
    /// <returns></returns>
    public virtual Task<int> ExecuteAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
    {
        return DbConnection.ExecuteAsync(sql, param, transaction, commandTimeout, commandType);
    }




    public void Dispose()
    {
        _db.Dispose();
    }

}


